import { reqgetSearchInfo } from '../api';

const state = {
  searchList: []
};
//mutions是唯一修改state的地方
const mutations = {
  SEARCHLIST(state, searchList) {
    state.searchList = searchList;
  },
};
//action|用户处理派发action地方的，可以书写异步语句、自己逻辑地方
const actions = {
  async getSearchList({ commit }, params = {}) {
    //当前这个reqgetSearchInfo函数在调取服务器数据的时候，传递一个默认的参数params，至少是一个空对象
    let result = await reqgetSearchInfo(params);
    if (result.code == 200) {
      commit("SEARCHLIST", result.data);
    }
  },
};
//计算属性，在项目中主要作用是：简化仓库中的数据
const getters = {
  //当前的state是当前仓库的state，并非是大仓库的state
  goodsList(state) {
    //假如网络不行，state.searchList.goodsList返回的应该是undefined
    return state.searchList.goodsList || [];
  },
  trademarkList(state) {
    //假如网络不行，state.searchList.trademarkList返回的应该是undefined
    return state.searchList.trademarkList || [];
  },
  attrsList(state) {
    //假如网络不行，state.searchList.attrsList返回的应该是undefined
    return state.searchList.attrsList || [];
  }
};
export default {
  state,
  mutations,
  actions,
  getters,
};
